package chaw.android.util;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CustomCalendar {
	static public Calendar calendar = Calendar.getInstance();
	
	static public String getCurrentTime(){
		String currentTime;
		
		String amPm;
		if (calendar.get(Calendar.AM_PM) == 0) amPm = "오전 ";
	    else amPm = "오후 ";
		
		currentTime = "" + (calendar.get(Calendar.MONTH)+1) + "월 " + calendar.get(Calendar.DAY_OF_MONTH) + "일 " + amPm + calendar.get(Calendar.HOUR) +
				"시 " + calendar.get(Calendar.MINUTE) + "분";
		
		return currentTime;
	}
	
	static public String parseTime(String currentTime){
		String result="";
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			Date date = format.parse(currentTime);
			calendar.setTime(date);
			String amPm = "";
			if (calendar.get(Calendar.AM_PM) == 0) amPm = "오전 ";
		    else amPm = "오후 ";
			
			result = "" + (calendar.get(Calendar.MONTH)+1) + "월 " + calendar.get(Calendar.DAY_OF_MONTH) + "일 " + amPm + calendar.get(Calendar.HOUR) +
					"시 " + calendar.get(Calendar.MINUTE) + "분";
		} catch (ParseException e) {
			result = "알수없는 시간";
		}
		
		return result;
	}
	
	static public String millisecondsToDate(long milliseconds){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		 
		Date resultdate = new Date(milliseconds);
		return parseTime( (format.format(resultdate).toString()) );
	}
	
	static public long dateToMilliseconds(String dateString){
		long when = 0;
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			Date date = format.parse(dateString);
			calendar.setTime(date);
			when = calendar.getTimeInMillis();
		}
		catch (ParseException e) {
			when = -1;
		}
		
		return when;
	}
}
